#include "showinfo.h"
#include "ui_showinfo.h"
#include <QDebug>
#include <QString>
#include <QMessageBox>

ShowInfo::ShowInfo(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ShowInfo)
{
    ui->setupUi(this);

    setFixedSize(793,567);

    model=new QSqlQueryModel;
    query=new QSqlQuery;
    model_1 = new QSqlQueryModel;
    query_1 = new QSqlQuery;
    query_2 = new QSqlQuery;
    query->exec("create table park(name vchar primary key,carnum vchar ,phone vchar,time vchar,seat vchar)");
    query_1->exec("create table wait(name vchar primary key,carnum vchar ,phone vchar)");
    query_2->exec("create table money(sum vchar primary key)");
    showView();
    count = 0;
    biandao = 0;
    sum = "0";
    QSqlQuery query7;
    query7.prepare("insert into money values(:a)");
    query7.bindValue(":a", sum);
    query7.exec();
}

ShowInfo::~ShowInfo()
{
    delete ui;
}

void ShowInfo::on_pushButton_in_clicked()
{

    newInCar=new InCar();
    newInCar->show();

}

void ShowInfo::on_pushButton_out_clicked()
{
    newOutCar=new OutCar();
    newOutCar->show();

}


void ShowInfo::showView(){
    model->setQuery("select * from park");
    model->setHeaderData(0,Qt::Horizontal,tr("name"));
    model->setHeaderData(1,Qt::Horizontal,tr("carid"));
    model->setHeaderData(2,Qt::Horizontal,tr("phone"));
    model->setHeaderData(3,Qt::Horizontal,tr("time"));
    model->setHeaderData(4,Qt::Horizontal,tr("seat"));
    ui->tableView_now->setModel(model);

    model_1->setQuery("select * from wait");
    model_1->setHeaderData(0, Qt::Horizontal, tr("name"));
    model_1->setHeaderData(1, Qt::Horizontal, tr("carid"));
    model_1->setHeaderData(2, Qt::Horizontal, tr("phone"));

    ui->tableView_now_2->setModel(model_1);
    QSqlQuery query1;
    query1.prepare("SELECT count(*) FROM park");
    query1.exec();
    query1.next();
    count = query1.value(0).toInt();//计算停车场车的数量
    QSqlQuery query2;
    query2.prepare("SELECT count(*) FROM wait");
    query2.exec();
    query2.next();
    biandao = query2.value(0).toInt();//计算便道车的数量

    QSqlQuery query3;
    query3.exec("select * from money ");
    //query3.exec();
    query3.first();
    sum = query3.value(0).toString();//总金额
    //qDebug()<<sum;

    int aa = 5;
    if (count < 5)
        aa = 5 - count;
    else aa = 0;
    ui->lineEdit_2->setText(QString::number(count));
    ui->lineEdit_3->setText(QString::number(biandao));
    ui->lineEdit_4->setText(QString::number(aa));
    ui->lineEdit_5->setText(sum);
}




void ShowInfo::on_pushButton_refresh_clicked()
{
    showView();
}

void ShowInfo::on_pushButton_clear_clicked()
{
    query = new QSqlQuery;
    query_1 = new QSqlQuery;
    query->exec("drop table park");

    //model=new QSqlQueryModel;
    query=new QSqlQuery;
    query->exec("create table park(name vchar primary key,carnum vchar ,phone vchar,time vchar,seat vchar)");
    query_1 = new QSqlQuery;
    query_1->exec("drop table wait");



    query_1->exec("create table wait(name vchar primary key,carnum vchar ,phone vchar)");
    QSqlQuery query4;
    query4.prepare("drop table money");
    query4.exec();
    QSqlQuery query5;
    query5.prepare("create table money(sum vchar primary key)");
    query5.exec();
    showView();

}

void ShowInfo::on_pushButton_find_clicked()
{

    QString phone2=ui->lineEdit->text();

    QString cmd("select * from park where phone=");
    cmd+=phone2;
    model->setQuery(cmd);
    model->setHeaderData(0,Qt::Horizontal,tr("name"));
    model->setHeaderData(1,Qt::Horizontal,tr("carid"));
    model->setHeaderData(2,Qt::Horizontal,tr("phone"));
    model->setHeaderData(3,Qt::Horizontal,tr("time"));
    model->setHeaderData(3,Qt::Horizontal,tr("seat"));
    ui->tableView_now->setModel(model);


    QString cmd2("select * from wait where phone=");
    cmd2+=phone2;
    model_1->setQuery(cmd2);
    model_1->setHeaderData(0,Qt::Horizontal,tr("name"));
    model_1->setHeaderData(1,Qt::Horizontal,tr("carid"));
    model_1->setHeaderData(2,Qt::Horizontal,tr("phone"));
    ui->tableView_now_2->setModel(model_1);

    ui->lineEdit->clear();
}

void ShowInfo::on_pushButton_exit_clicked()
{
    close();
}
